<?
/* / / / / / ENGINE  ROOM / / / / / / /
|	          CORE CLASS              |
|      (aka. public superclass)       |
|                                     |
| version:     0.2.0                  |
| last edit:   20100201               |
| package:     beta 2                 |
/ / / / / / / / / / / / / / / / / / / /

SOME DOCUMENTATION:

User and private pages control.
-------------------------------
User session control:
$Engine->user_session();
*/


class SOCIAL extends ENGINE {
	function SOCIAL( $type ) {
		$this->user_session();
	}
	
/** 	SUPER FUNCTIONS (+DB)
*  */


	public function activate($objid, $objtype) {
		global $DB;
		$DB->tube('UPDATE glue_social SET active = 1 WHERE objid = '.$objid . ' AND objtype = ' . $objtype); // Set active to 1 in glue_social.
	}
	
	
	// SINGLE RETURNS INTEGER, MULTI RETURNS NUM ARRAY, 
	/* // Wim's comment: not using this anymore. feel free to uncomment if you do.
	public function glue_array ($parenttype, $parentid, $objtype, $objid = 0, $order = "objid DESC", $multi = true) {
		global $DB;
		if( $n = $parenttype) $q[] = "parenttype=$n";
		if( $n = $objtype) $q[] = "objtype=$n";
		if( $n = $parentid) $q[] = "parentid=$n";
		if( $n = $objid)$q[] = "objid=$n";
		$what = ($parentid)? "objid" : "parentid";
		
		if( $raw = $DB->parse( $what, "glue_social", implode( " && ", $q) . " order by $order", $multi) )
			if( $multi ) foreach( $raw as $node ) $result[] = (int) $node[ $what ];
			else $result = (int) $raw[ $what ];
		
		return $result;
	}
	*/
	
	
	

/** 	SOCIAL: URL SHORTENING
 */

	function do_curl_request($url, $param) {
		$api = $url."?".$param;

		$session = curl_init();
		curl_setopt($session, CURLOPT_URL, $api);
		curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
		$data = curl_exec($session);
		curl_close($session);

		return $data;
	}
	
	/* URL shortening using is.gd */
	function make_isgd_url($longurl) {
		$url = "http://is.gd/api.php";
		$variable = "longurl";
		$shorturl = $this->do_curl_request($url, $variable."=".$longurl);
		return $shorturl;
	}
	
	/* URL shortening using bitly  */
	function make_bitly_url($url,$login,$appkey,$format = 'xml',$history=1,$version = '2.0.1')
	{
		//create the URL
		$bitly = 'http://api.bit.ly/shorten';
		$param = 'version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;

		// send to cURL
		$response = $this->do_curl_request($bitly,$param);

		//parse depending on desired format
		if(strtolower($format) == 'json')
		{
			$json = @json_decode($response,true);
			return $json['results'][$url]['shortUrl'];
		}
		else //xml
		{
			$xml = simplexml_load_string($response);
			return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
		}
	}
}
?>